/**
 * @file bsp_aht21_handler.h
 * @author liufeng (liufeng@injoinic.com)
 * @brief aht21 handler with FreeRTOS.
 * @version 0.1
 * @date 2024-11-06
 *
 * @copyright Copyright (c) 2024  injoinic
 *
 */

/* Inlcude ------------------------------------------------------------------ */

#include "bsp_aht21_driver.h"
#include "stdbool.h"
#include "stdint.h"

/* Define ------------------------------------------------------------------- */

// event type enum
typedef enum
{
    GET_TEMP,
    GET_HUMI,
    GET_BOTH
} aht21HandlerEventEnum;

// temprature and humidity date type
typedef struct
{
    aht21HandlerEventEnum event;
    float temprature;
    float humidity;
} aht21TempAndHumiType;

// aht21 handler和driver需要的所有外部函数接口
typedef struct
{
    /* iic相关函数的接口 */
    void (*pf_iic_init)(void);   // iic init interface
    void (*pf_iic_deinit)(void); // iic deinit interface
    uint8_t (*pf_iic_write_bytes)(uint8_t addr, uint8_t* buff, uint8_t len); // iic write
    uint8_t (*pf_iic_read_bytes)(uint8_t addr, uint8_t* buff, uint8_t len);  // iic read

    /* 关中断或者进入临界区的函数接口 */
    void (*pf_disable_irq)(void); // disable irq(without OS) or enter critical(with OS)
    void (*pf_enable_irq)(void);  // enable irq(without OS) or exit critical(with OS)

    /* 时基相关的函数接口 */
    void (*pf_delay_ms)(uint32_t); // blocking delay(without OS) or unblocking delay(with OS)
    uint32_t (*pf_get_tick)(void); // get tick from RTOS or HAL lib

    /* 队列操作相关的函数接口 */
    void* (*pf_queue_create)(uint32_t item_num, uint32_t item_size);           // from RTOS
    uint8_t (*pf_queue_put)(void* queue_handle, void* item, uint32_t timeout); // from RTOS
    uint8_t (*pf_queue_get)(void* queue_handle, void* msg, uint32_t timeout);  // from RTOS
} aht21_input_func;

// aht21 handler需要的所有外部函数接口
typedef struct
{
    /* 时基相关的函数接口 */
    void (*pf_delay_ms)(uint32_t); // unblocking delay(with OS)
    uint32_t (*pf_get_tick)(void); // get tick from RTOS or HAL lib

    /* 队列操作相关的函数接口 */
    void* (*pf_queue_create)(uint32_t item_num, uint32_t item_size);           // from RTOS
    uint8_t (*pf_queue_put)(void* queue_handle, void* item, uint32_t timeout); // from RTOS
    uint8_t (*pf_queue_get)(void* queue_handle, void* msg, uint32_t timeout);  // from RTOS
} aht21_handler_input_func;

// aht21 handler typedef
typedef struct
{
    aht21_handler_input_func* func_port; // all outside function port

    aht21_type* aht21_instance;          // from bsp_aht21_driver
    void* event_queue_handle;            // event queue handle
    void* data_queue_handle;             // data queue handle

    bool is_init;                        // aht21Handler init or not
    uint32_t timestamp;                  // timestamp of last temprature and humidity get
} aht21_handler_type;

/* Declaration -------------------------------------------------------------- */

extern aht21_handler_type aht21Handler;
void aht21HandlerTask(void* argument);
